#!/bin/bash
# echo the call so we can see what's going on
echo "$0 $1 $2 $3 $4 $5 $6"

# copy the EXP file so we have a fixed filename
cp `cat GSAS_EXP`.EXP PHASES_SIM.EXP

# make a new tex file
echo "\documentclass{article}" > `cat GSAS_EXP`.tex
echo "\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}" >> `cat GSAS_EXP`.tex
echo "\usepackage{graphicx}" >> `cat GSAS_EXP`.tex
echo "\begin{document}" >> `cat GSAS_EXP`.tex
echo " " >> `cat GSAS_EXP`.tex
echo "This file shows diffraction patterns for \verb=`grep PNAM PHASES_SIM.EXP | tail -c68 | head -c65 | sed 's/[ ]*$//g'`=. Simulation parameters:" >> `cat GSAS_EXP`.tex
echo "\begin{itemize}" >> `cat GSAS_EXP`.tex
echo "\item Instrument parameter file \verb=`grep 'HST  1  IFIL ' PHASES_SIM.EXP | tail -c68 | head -c65 | sed 's/[ ]*$//g'`=.  " >> `cat GSAS_EXP`.tex
echo "Bank: `grep 'HST  1 BANK ' PHASES_SIM.EXP | tail -c68 | head -c65 | sed 's/[ ]*$//g'`." >> `cat GSAS_EXP`.tex
echo "\item Space group: \verb=`grep 'CRS1  SG SYM' PHASES_SIM.EXP | tail -c68 | head -c65 | sed 's/[ ]*$//g'`=" >> `cat GSAS_EXP`.tex
echo "\item Lattice parameters: \verb=`grep 'CRS1  ABC   ' PHASES_SIM.EXP | tail -c69 | head -c65 | sed 's/[ ]*$//g'`= " >> `cat GSAS_EXP`.tex
echo "\verb=`grep 'CRS1  ANGLES' PHASES_SIM.EXP | tail -c69 | head -c65 | sed 's/[ ]*$//g'`=." >> `cat GSAS_EXP`.tex
echo "\end{itemize} " >> `cat GSAS_EXP`.tex
echo " " >> `cat GSAS_EXP`.tex
# Plot the pattern normalized and without normalization
echo "z" > temp.txt
echo "e" >> temp.txt
echo "b m d t" >> temp.txt

# determine whether we have TOF or CW
echo "grep..."
grep HTYPE `grep "1  IFIL" PHASES_SIM.EXP | awk '{ print $4}'` | head -c 17 > htype.txt
echo "if..."
if [ "`tail -c 1 htype.txt`" == "C" ]; then
    echo "We have a constant wavelength instrument no need to normalize with incident intensity."
    rm S_NONORM.EPS
    echo "n s p" >> temp.txt
    echo "$1 $2 a" >> temp.txt
    echo "S_NONORM" >> temp.txt
    echo "S_NONORM" >> temp.txt
    echo " " >> temp.txt
    echo "n" >> temp.txt
    echo " " >> `cat GSAS_EXP`.tex
    echo "Pattern:\\\\" >> `cat GSAS_EXP`.tex
    echo "\includegraphics[width=160mm]{S_NONORM.EPS}" >> `cat GSAS_EXP`.tex

else
    if [ "`tail -c 1 htype.txt`" == "T" ]; then
	echo "We have a time-of-flight instrument, give both normalized and non-normalized intensity."
	rm S_NONORM.EPS
	rm S_NORM.EPS
	echo "n s p" >> temp.txt
	echo "$1 $2 a" >> temp.txt
	echo "S_NONORM" >> temp.txt
	echo "S_NONORM" >> temp.txt
	echo " " >> temp.txt
	echo "n" >> temp.txt
	echo " " >> `cat GSAS_EXP`.tex
	echo "Pattern without normalization:\\\\" >> `cat GSAS_EXP`.tex
	echo "\includegraphics[width=120mm]{S_NONORM.EPS}" >> `cat GSAS_EXP`.tex

	echo "i p" >> temp.txt
	echo "$1 $2 a" >> temp.txt
	echo "S_NORM" >> temp.txt
	echo "S_NORM" >> temp.txt
	echo " " >> temp.txt
	echo "n" >> temp.txt
	echo "\\\\Pattern normalized by incident intensity:\\\\" >> `cat GSAS_EXP`.tex
	echo "\includegraphics[width=120mm]{S_NORM.EPS}" >> `cat GSAS_EXP`.tex
    else
	echo "We have a unknown type of instrument - need debugging!"
	read -p "Break here with Ctrl-c and debug!"
    fi
fi


echo "x" >> temp.txt

nice powplot `cat GSAS_EXP`.EXP < temp.txt > out.txt

# Generate a peak list for the first 20 peaks
# remove LST file, we don't care
rm `cat GSAS_EXP`.LST
echo "1" > temp.txt
echo "l" >> temp.txt
echo "0" >> temp.txt
nice reflist `cat GSAS_EXP`.EXP < temp.txt > out.txt

# process the reflection list a bit into a nice Latex table
grep Iref `cat GSAS_EXP`.LST -A 30 | head -n 31 | tail -n 30 | awk '{print $2, $3, $4, $5, $7}' > temp.txt
echo " " >> `cat GSAS_EXP`.tex
echo " " >> `cat GSAS_EXP`.tex
echo "\begin{tabular}{|ccc|c|l|}" >> `cat GSAS_EXP`.tex
echo "\hline" >> `cat GSAS_EXP`.tex
echo "h&k&l&multiplicity&d-spacing [A]\\\\" >> `cat GSAS_EXP`.tex
echo "\hline" >> `cat GSAS_EXP`.tex
cat temp.txt | sed -e 's/ /\&/g' -e 's/$/\\\\/g' >> `cat GSAS_EXP`.tex
echo "\hline" >> `cat GSAS_EXP`.tex
echo "\end{tabular}" >> `cat GSAS_EXP`.tex

# delete the temporary EXP file
rm PHASES_SIM.EXP

# end the Latex source and compile to PDF
echo "\end{document}" >> `cat GSAS_EXP`.tex
echo "Generating DVI file..."

# Produce PDF file with information
latex `cat GSAS_EXP`.tex
if [ "$OS" = "Windows_NT" ]; then
	echo "I think I run on a Windows box..."
	dvips `cat GSAS_EXP`
	ps2pdf `cat GSAS_EXP`.ps
else
	echo "I think I run on a Unix box..."
	echo "Generating PDF file..."
	dvipdf `cat GSAS_EXP`.dvi
fi

